<?php
class Admin_LeaveController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pTid = empty($p['t_id'])?'':Tool_Fnc::safe_string($p['t_id']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        $pKeyword = empty($p['keyword'])?'':Tool_Fnc::safe_string($p['keyword']);
        $pLeavetime = empty($p['leavetime'])?'':Tool_Fnc::safe_string($p['leavetime']);

        $tDIMO = new DepartmentinfoModel;
        $tRIMO = new RoleinfoModel;
        $tWMO = new WorkerModel;
        $tLMO = new LeavedModel;
        $tAMO = new AdminModel;
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pDepartmentID = empty($p['department_id'])?Tool_Fnc::ajaxMsg('系统错误'):Tool_Fnc::safe_string($p['department_id']);
            $tSql = 'select id,name from '.$tWMO->table.' where department = '.$pDepartmentID;
            $tDatas = $tWMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }

        $tWhere = ' 1 and l.status = 1';

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and l.status = 1 and w.department = '.$tAData[0]['department'];
            }
        }

        if(!empty($pDepartment)){
            $tWhere .= ' and w.department = '.$pDepartment;
        }
        if(!empty($pLeavetime)){
            $tWhere .= ' and l.leavetime like \'%'.$pLeavetime.'%\'';
        }
        if(!empty($pKeyword)){
            $tWhere .= ' and w.name like \'%'.$pKeyword.'%\' or w.phone like \'%'.$pKeyword.'%\'';
        }

        $tSql = 'select count(*) c from '.$tLMO->table.' l left join '.$tWMO->table.' w on l.w_id = w.id left join '.$tAMO->table.' a on a.w_id = w.id left join '.$tDIMO->table.' di on w.department = di.num where '.$tWhere;
        $tCount = $tLMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:Tool_Fnc::safe_string($tCount[0]['c']);
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tSql = 'select l.id id,w.name name,w.phone phone,di.introduce department,ri.introduce workertype,l.leavetime leavetime,l.leavedays leavedays from '.$tLMO->table.' l left join '.$tWMO->table.' w on l.w_id = w.id left join '.$tAMO->table.' a on a.w_id = w.id left join '.$tDIMO->table.' di on w.department = di.num left join '.$tRIMO->table.' ri on w.worker_type = ri.num where '.$tWhere.' order by l.id desc limit '.$tPage->limit();
            $tDatas = $tLMO->query($tSql);
        }
        //所在部门
        $tDDatas = $tDIMO->field('num,introduce')->fList();
        //编辑页面显示
        if(!empty($pTid)){
            $tSql = 'select w.id w_id,l.id id,l.leavetime leavetime,l.leavedays leavedays,w.department department,w.name name from '.$tLMO->table.' l left join '.$tWMO->table.' w on l.w_id = w.id where l.id = '.$pTid; 
            $tTData = $tLMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tTData[0]);
        }

        $this->assign('pDepartment',$pDepartment);
        $this->assign('pKeyword',$pKeyword);
        $this->assign('pTraindate',$pTraindate);
        $this->assign('tDatas',$tDatas);
        $this->assign('tDDatas',$tDDatas);
    
    }
    public function addAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pWorker = empty($p['worker'])?Tool_Fnc::ajaxMsg('请先选择员工姓名'):Tool_Fnc::safe_string($p['worker']);
            $pLeavetime = empty($p['leavetime'])?Tool_Fnc::ajaxMsg('请假时间不能为空'):Tool_Fnc::safe_string($p['leavetime']);
            $pLeavedays = empty($p['leavedays'])?Tool_Fnc::ajaxMsg('请假天数不能为空'):Tool_Fnc::safe_string($p['leavedays']);
            $tTime = time();

            $tData = array(
                'w_id' => $pWorker,
                'leavetime' => $pLeavetime,
                'leavedays' => $pLeavedays,
                'created' => $tTime,
                'status' => 1,
            );
            $tLMO = new LeavedModel;
            if(!$tId = $tLMO->insert($tData)){
                Tool_Fnc::ajaxMsg('添加失败');
            }
            Tool_Fnc::ajaxMsg('添加成功',1);
        }
    
    }
    public function editAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pTid = empty($p['tId'])?Tool_Fnc::ajaxMsg('tId不能为空'):Tool_Fnc::safe_string($p['tId']);
            $pWorker = empty($p['worker'])?Tool_Fnc::ajaxMsg('请先选择员工姓名'):Tool_Fnc::safe_string($p['worker']);
            $pLeavetime = empty($p['leavetime'])?Tool_Fnc::ajaxMsg('请假时间不能为空'):Tool_Fnc::safe_string($p['leavetime']);
            $pLeavedays = empty($p['leavedays'])?Tool_Fnc::ajaxMsg('请假天数不能为空'):Tool_Fnc::safe_string($p['leavedays']);
            $tTime = time();

            $tData = array(
                'w_id' => $pWorker,
                'leavetime' => $pLeavetime,
                'leavedays' => $pLeavedays,
                'updated' => $tTime,
                'status' => 1,
                'id' => $pTid,
            );
            $tLMO = new LeavedModel;
            if(!$tLMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }
            Tool_Fnc::ajaxMsg('修改成功',1);
        }
    
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);
        $tLMO = new LeavedModel;
        $tSql = 'update '.$tLMO->table.' set status = 2 where id = '.$pId;
        if(!$tLMO->exec($tSql)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    }
    
}
?>
